import pandas as pd
import os
import matplotlib.pyplot as plt

file = r'Data\Derivational_EVUsage_Data.csv'
df = pd.read_csv(file)
fig_path = r'特征衍生\Picture'
os.chdir(fig_path)

# 分析'Time of Day'('Early Morning', 'Morning', 'Afternoon', 'Evening')的分布
count = df['Time of Day'].value_counts() # 计算“Time of Day”列中每个时间点的出现次数
print(count)
# 绘制'Time of Day'的分布图
plt.pie(count, labels=count.index, autopct='%1.1f%%') # 绘制饼图，显示每个时间点的比例
plt.savefig('Time of Day.png') # 保存图片
plt.clf() # 清除上面的绘图

# 分析'Time of Day'与'Average Cost per kWh'的关系
df.groupby('Time of Day')['Average Cost per kWh'].mean().plot(kind='bar', color='skyblue') # 绘制柱状图，显示每个时间点的'Average Cost per kWh'的平均值
plt.title('Average Cost per kWh by Time of Day') # 添加标题
plt.xlabel('Time of Day') # 添加横轴标签
plt.ylabel('Average Cost per kWh') # 添加纵轴标签
plt.xticks(rotation=0, ha='center') # 横轴标签旋转90度，水平居中
plt.tight_layout() # 自动调整子图参数，使之填充整个图像区域
plt.savefig('Average Cost per kWh by Time of Day.png') # 保存图片
plt.clf() # 清除上面的绘图

# 分析'is_weekday'中,为1的占比
weekday = df['is_weekday'].value_counts() # 计算“is_weekday”列中每个时间点的出现次数
print(weekday)
# 清除上面的绘图
plt.clf()
# 绘制饼状图，并加上标题、标签和百分比, 标签中的0、1改为工作日和周末
plt.pie(weekday, labels=['weekday', 'weekend'], autopct='%1.1f%%') # 绘制饼图，显示每个时间点的比例
plt.title('weekday vs weekend') # 添加标题
plt.savefig('is_weekday.png') # 保存图片
plt.clf() # 清除上面的绘图

# 分析'Season'列中,每个季节的占比
season = df['Season'].value_counts() # 计算“Season”列中每个季节的出现次数
print(season)
# 绘制饼状图，并加上标题、标签和百分比
plt.pie(season, labels=season.index, autopct='%1.1f%%') # 绘制饼图，显示每个时间点的比例
plt.title('Season') # 添加标题
plt.savefig('Season.png') # 保存图片
plt.clf() # 清除上面的绘图

# 分析'Charging Efficiency'与'Season'的关系
describe = df.groupby('Season')['Charging Efficiency'].describe() # 计算每个季节的'Charging Efficiency Level'的描述性统计量
print(describe)
# 绘制每个季节的'Charging Efficiency'的柱状图
describe['mean'].plot(kind='bar', color='skyblue') # 绘制柱状图，显示每个时间点的'Average Cost per kWh'的平均值
plt.title('Charging Efficiency by Season') # 添加标题
plt.xlabel('Season') # 添加横轴标签
plt.ylabel('Charging Efficiency') # 添加纵轴标签
plt.xticks(rotation=0, ha='center') # 横轴标签旋转90度，水平居中
plt.tight_layout() # 自动调整子图参数，使之填充整个图像区域
plt.savefig('Charging Efficiency by Season.png') # 保存图片
plt.clf() # 清除上面的绘图

# 分析'Energy (kWh)'与'Season'的关系
describe = df.groupby('Season')['Energy (kWh)'].describe() # 计算每个季节的'Energy (kWh)'的描述性统计量
print(describe)
# 绘制每个季节的'Energy (kWh)'的散点图
plt.scatter(df['Season'], df['Energy (kWh)'], color='skyblue') # 绘制散点图，显示每个时间点的'Average Cost per kWh'的平均值
plt.title('Energy (kWh) by Season') # 添加标题
plt.xlabel('Season') # 添加横轴标签
plt.ylabel('Energy (kWh)') # 添加纵轴标签
plt.savefig('Energy (kWh) by Season.png') # 保存图片
plt.clf() # 清除上面的绘图

# 分析'is_peak'列中, 为1的占比
'''
lambda x: 1 if x >= 7 and x <= 9 or x >= 17 and x <= 19 else 0 '''
peak = df['is_peak'].value_counts() # 计算“is_peak”列中每个时间点的出现次数
print(peak)
# 绘制饼状图，并加上标题、标签和百分比
plt.pie(peak, labels=['peak', 'non-peak'], autopct='%1.1f%%') # 绘制饼图，显示每个时间点的比例
plt.title('peak vs non-peak') # 添加标题
plt.savefig('is_peak.png') # 保存图片
plt.clf() # 清除上面的绘图

# 分析'is_peak'与'Season'的关系
describe = df.groupby('Season')['is_peak'].describe() # 计算每个季节的'is_peak'的描述性统计量
print(describe)
# 绘制每个季节的'is_peak'的柱状图
describe['mean'].plot(kind='bar', color='skyblue') # 绘制柱状图，显示每个时间点的'Average Cost per kWh'的平均值
plt.title('is_peak by Season') # 添加标题
plt.xlabel('Season') # 添加横轴标签
plt.ylabel('is_peak') # 添加纵轴标签
plt.xticks(rotation=0, ha='center') # 横轴标签旋转90度，水平居中
plt.tight_layout() # 自动调整子图参数，使之填充整个图像区域
plt.savefig('is_peak by Season.png') # 保存图片
plt.clf() # 清除上面的绘图

# 分析'Average Energy Consumption'与'is_peak'的关系
describe = df.groupby('is_peak')['Average Energy Consumption'].describe() # 计算每个季节的'Average Energy Consumption'的描述性统计量
print(describe)
# 去除异常值，假设我们认为超过 20 的值为异常值
df_filtered = df[df['Average Energy Consumption'] <= 20]
# 绘制散点图（Scatter Plot）
plt.figure(figsize=(10, 6))
plt.scatter(df_filtered['is_peak'], df_filtered['Average Energy Consumption'], color='skyblue')
plt.title('Average Energy Consumption by is_peak')
plt.xlabel('is_peak')
plt.ylabel('Average Energy Consumption (kWh)')
plt.ylim(0, 25)  # 手动设置纵坐标范围，根据实际数据调整
plt.savefig('Average Energy Consumption by is_peak.png')
plt.clf() # 清除上面的绘图

# 分析'Port Number'[1, 2]与'Average Energy Consumption'的关系
describe = df.groupby('Port Number')['Average Energy Consumption'].describe() # 计算每个季节的'Average Energy Consumption'的描述性统计量
print(describe)
# 去除异常值，假设我们认为超过 20 的值为异常值
df_filtered = df[df['Average Energy Consumption'] <= 20]
# 绘制散点图（Scatter Plot）
plt.figure(figsize=(10, 6))
plt.scatter(df_filtered['Port Number'], df_filtered['Average Energy Consumption'], color='skyblue')
plt.title('Average Energy Consumption by Port Number')
plt.xlabel('Port Number')
plt.ylabel('Average Energy Consumption (kWh)')
plt.ylim(0, 25)  # 手动设置纵坐标范围，根据实际数据调整
plt.savefig('Average Energy Consumption by Port Number.png')
plt.clf() # 清除上面的绘图